﻿jQuery(function( $ ){
    $(document).ready(function () {
        //************** begin flagging system *****************
        $(".flagComment").click(function () {
            var commentID = $(this).attr("id");
            $.post("/Comment/Flag", { id: commentID },
            function (obj) {
                alert(obj.Message);
            }
        );
        });

        $('.dateTimePicker').datepicker({
            duration: '',
            showTime: true,
            constrainInput: false,
            showOn: 'both',
            buttonImage: '/Content/Images/calendar.png',
            buttonImageOnly: true
        });

        $(".flagPost").click(function () {
            var postID = $(this).attr("id");
            $.post("/Post/Flag", { id: postID },
            function (obj) {
                alert(obj.Message);
            }
        );
        });

        //************** end flagging system *****************

        //************** begin star rating system *****************
        $(".rating.stars.active").mouseover(function () {
            var span = $(this).parent("span");
            var newRating = $(this).attr("value");
            setRating(span, newRating);
        });

        $(".rating.stars.active").mouseout(function () {
            var span = $(this).parent("span");
            var rating = span.attr("rating");
            setRating(span, rating);
        });

        $(".rating.stars.active").click(function () {
            var span = $(this).parent("span");
            var newRating = $(this).attr("value");
            var text = span.children("span");
            var pID = span.attr("post");
            $.post("/Post/SaveRating", { postID: pID, rating: newRating },
            function (obj) {
                if (obj.Success) {
                    text.html("Currently rated " + obj.Result.AverageScore + " by " + obj.Result.Votes + " people"); //modify the text
                    span.attr("rating", obj.Result.AverageScore); //set the rating attribute
                    setRating(span, obj.Result.AverageScore); //update the display
                    alert("Thank you, your vote was casted successfully.");
                }
                else {
                    alert(obj.Message); //failure, show message
                }
            }
        );
        });

        function setRating(span, rating) {
            span.find('.rating.stars').each(function () {
                var value = parseFloat($(this).attr("value"));
                var imgSrc = $(this).attr("class");
                if (value <= rating)
                    $(this).attr("class", imgSrc.replace("_off", "_on"));
                else
                    $(this).attr("class", imgSrc.replace("_on", "_off"));
            });
        }
        //************** end star rating system *****************

        //************** start widgets *****************
        $(".widget_delete").click(function () {
            var parentDiv = $(this).parents(".widget");
            var widgetID = parentDiv.attr("id");
            $.post("/Widget/Delete", { id: widgetID }, function (obj) {
                if (obj.Success == true) {
                    parentDiv.remove();
                }
                else {
                    alert(obj.Message);
                }
            });
        });
        //************** end widgets *****************	

        $(".widget-ul").sortable({
            revert: true,
            stop: function (event, ui) {
                var rowIdx = $(event.target).children().index(ui.item[0]);
                var widgetID = $(ui.item[0]).children(".widget").attr("id");
                $.post("/Widget/SavePosition", { id: widgetID, row: rowIdx, col: 1 }, function (obj) {
                    if (obj.Success == false) {
                        alert(obj);
                    }
                });
            }
        });

        //************** posts *****************	
        $(".deletePost").click(function () {
            var id = $(this).attr("id");
            var title = $(this).attr("name");
            var formatStr = $(this).attr("formatString");
            var message = formatStr.replace('{0}', title);
            var postDiv = $(this).parents("div.post");

            if (confirm(message) == false)
                return false;

            $.post("/Post/Delete/", { 'id': id }, function (obj) {
                if (obj.Success == true) {
                    document.location.href = "/Post/Index";
                }
                else {
                    alert(obj.Message);
                }
            });

            return false;
        });

        $('#jsddm > li').bind('mouseover', jsddm_open)
        $('#jsddm > li').bind('mouseout', jsddm_timer)

        var timeout = 500;
        var closetimer = 0;
        var ddmenuitem = 0;

        function jsddm_open() {
            jsddm_canceltimer();
            jsddm_close();
            ddmenuitem = $(this).find('ul').css('visibility', 'visible');
        }

        function jsddm_close()
        { if (ddmenuitem) ddmenuitem.css('visibility', 'hidden'); }

        function jsddm_timer()
        { closetimer = window.setTimeout(jsddm_close, timeout); }

        function jsddm_canceltimer() {
            if (closetimer) {
                window.clearTimeout(closetimer);
                closetimer = null;
            }
        }

        document.onclick = jsddm_close;
    });
});